package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.AllArgsConstructor;
import org.jeecg.modules.hc.entity.*;
import org.jeecg.modules.hc.mapper.*;
import org.jeecg.modules.hc.service.IHcCompanyFoodSpecificationsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 企业订餐菜品规格设置
 * @Author: jeecg-boot
 * @Date:   2023-07-10
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyFoodSpecificationsServiceImpl extends ServiceImpl<HcCompanyFoodSpecificationsMapper, HcCompanyFoodSpecifications> implements IHcCompanyFoodSpecificationsService {

    private HcCompanyMerchantInfoMapper hcCompanyMerchantInfoMapper;
    private HcCompanyFoodListMapper hcCompanyFoodListMapper;
    private HcMerchantMapper hcMerchantMapper;
    private HcCompanyFoodSpecificationsMapper hcCompanyFoodSpecificationsMapper;

    @Override
    public List<HcMerchant> getListNewByCompanyId(String companyId) {

        LambdaQueryWrapper<HcCompanyMerchantInfo> companyMerchantInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        companyMerchantInfoLambdaQueryWrapper.eq(HcCompanyMerchantInfo::getCompanyId,companyId);
        //获取企业对应的商户关系
        List<HcCompanyMerchantInfo> hcCompanyMerchantInfos = hcCompanyMerchantInfoMapper.selectList(companyMerchantInfoLambdaQueryWrapper);
        List<HcMerchant> merchants = new ArrayList<>();
        for (HcCompanyMerchantInfo hcCompanyMerchantInfo : hcCompanyMerchantInfos) {
            HcMerchant hcMerchant = hcMerchantMapper.selectOne(new LambdaQueryWrapper<HcMerchant>().eq(HcMerchant::getId, hcCompanyMerchantInfo.getMerchantId()));

            //获取商户对应的展示的菜品
            LambdaQueryWrapper<HcCompanyFoodList> foodListLambdaQueryWrapper = new LambdaQueryWrapper<>();
            foodListLambdaQueryWrapper.eq(HcCompanyFoodList::getCompanyId,companyId).eq(HcCompanyFoodList::getMerchantId,hcMerchant.getId());
            List<HcCompanyFoodList> foodLists = hcCompanyFoodListMapper.selectList(foodListLambdaQueryWrapper);
            //规格信息
            List<HcCompanyFoodList> newRecords = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(foodLists)){
                foodLists.forEach( r -> {
                    String isUseSpecifications = r.getIsUseSpecifications();
                    if (isUseSpecifications.equalsIgnoreCase("yes")){
                        LambdaQueryWrapper<HcCompanyFoodSpecifications> specificationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        specificationsLambdaQueryWrapper.eq(HcCompanyFoodSpecifications::getFoodsId,r.getId());
                        List<HcCompanyFoodSpecifications> all = hcCompanyFoodSpecificationsMapper.selectList(specificationsLambdaQueryWrapper);
                        List<HcCompanyFoodSpecifications> specificationsList = all.stream().filter(s -> StringUtils.isEmpty(s.getParentId())).collect(Collectors.toList());
                        List<HcCompanyFoodSpecifications> foodSpecifications = getNextFoodSpecifications(specificationsList, all);
                        r.setSpecifications(foodSpecifications);
                    }
                    newRecords.add(r);
                });
            }
            hcMerchant.setFoodLists(newRecords);
            merchants.add(hcMerchant);
        }
        return merchants;
    }
    private List<HcCompanyFoodSpecifications> getNextFoodSpecifications(List<HcCompanyFoodSpecifications> specifications, List<HcCompanyFoodSpecifications> all){
        //遍历上级规格找到对应的下级规格
        specifications.forEach(s -> {
            List<HcCompanyFoodSpecifications> specifications1 = new ArrayList<>();
            all.forEach( a -> {
                if (StringUtils.isNotBlank(a.getParentId())){
                    if (a.getParentId().equals(s.getId())){
                        specifications1.add(a);
                        String isUseSpecifications = a.getIsUseSpecifications();
                        if (isUseSpecifications.equals("yes")){
                            List<HcCompanyFoodSpecifications> specificationList = a.getNextSpecifications();
                            getNextFoodSpecifications(specificationList,all);
                        }
                    }
                }
            });
            s.setNextSpecifications(specifications1);
        });
        return specifications;
    }
}
